package com.adolph.netty.http;

import com.adolph.netty.nettydemo.NettyServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.SingleThreadEventExecutor;

/**
 * 〈功能概述〉<br>
 *
 * @author: wangxiangjun
 * @date: 2020-10-10 16:23
 */

public class NettyHttpServer {
    public static void main(String[] args) throws InterruptedException {
        //
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            //启动器
            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class)//AbstractBootstrap的方法
                    .option(ChannelOption.SO_BACKLOG,128)//设置线程队列得到连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE,Boolean.TRUE)//设置保持活动连接状态
                    .childHandler(new ServerChannelInitializer());
            //绑定端口并且同步
            ChannelFuture sync = bootstrap.bind(8888).sync();
            //Feature-listener机制：注册监听器，处理关心的事情
            sync.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {

                }
            });
            //对关闭通道进行监听
            sync.channel().closeFuture().sync();
        }catch (Exception e){

        }finally {
            workGroup.shutdownGracefully();
        }



    }
}
